package com.wno704.quartz.task;

import com.wno704.quartz.entity.Job;
import com.wno704.quartz.service.JobService;
import com.wno704.system.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.*;


/**
 * @Description: 数据抽取-数据库之间操作定时器
 * @Name:
 * @Param:
 * @return:
 * @Author: wno704
 * @Date: 2021/6/7
 */
@Slf4j
@Component
public class SendData {

    @Autowired
    private JobService jobService;

    public void transData(String params) {
        Job job = jobService.findJob(Long.parseLong(params));
        log.info("{}-{}----start", job.getRemark(), "SendData");
        log.info("{}----{}----{}----{}----{}----{}----{}", params, job.getTemplateSrcName(), job.getTemplateDstName(), job.getTemplateDstTwoName(), job.getSrcCommd(), job.getDstCommd(), job.getDstTwoCommd());

        //源数据插入目标库 1：A查询-B操作 (普通)
        if (job.getTransType().equals("1")) {
            transDataFromSrcToDst(job);
        }

        //目标库脚本定期更新  2：A/B/C操作
        else if (job.getTransType().equals("2")) {
            updateDstData(job);
        }

        //源数据插入目标库 Batch 3：A查询-B操作
        else if (job.getTransType().equals("3")) {
            transDataFromSrcToDstBatch(job);
        }

        //源数据插入目标库 Batch 4: A查询-B查询-C操作
        else if (job.getTransType().equals("4")) {
            transDataFromSrcToDstBatchTwo(job);
        }

        //源数据插入目标库 Batch 5: A查询-B操作-C操作
        else if (job.getTransType().equals("5")) {
            transDataFromSrcToDstTwoBatch(job);
        }

        //源数据插入目标库 Batch 6: A操作-B查询-C操作
        else if (job.getTransType().equals("6")) {
            transDataFromDstToDstTwoBatch(job);
        }

        //源数据插入目标库 Batch 7: A查询-C操作  B查询-C操作
        else if (job.getTransType().equals("7")) {
            transDataFromSrcandDstToDstTwoBatch(job);
        }

        //源数据插入目标库 Batch 8: A查询-B操作  --C操作
        else if (job.getTransType().equals("8")) {
            transDataFromSrcandSrcToDstAndDstTwo(job);
        }


        log.info("{}-{}----end", job.getRemark(), "SendData");
    }

    //8：A操作-B查询-C操作
    private void transDataFromSrcandSrcToDstAndDstTwo(Job job) {
        JdbcTemplate jdbcTemplateSrc = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateSrcName());
        JdbcTemplate jdbcTemplateDst = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateDstName());
        JdbcTemplate jdbcTemplateTwoDst = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateDstTwoName());
        String srcCmd = job.getSrcCommd();
        String dstCmd = job.getDstCommd();
        String dstTwoCmd = job.getDstTwoCommd();

        List<Map<String, Object>> srcData = jdbcTemplateSrc.queryForList(srcCmd);
        if (srcData.size() > 0) {
            List<Object[]> listDst = new ArrayList<Object[]>();
            for (Map<String, Object> mp : srcData) {
                listDst.add(mp.values().toArray());
            }
            int[] ans = jdbcTemplateDst.batchUpdate(dstCmd, listDst);
        }
        int[] ansTwo = jdbcTemplateTwoDst.batchUpdate(dstTwoCmd);
    }

    //7: A查询-C操作  B查询-C操作
    private void transDataFromSrcandDstToDstTwoBatch(Job job) {
        JdbcTemplate jdbcTemplateSrc = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateSrcName());
        JdbcTemplate jdbcTemplateDst = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateDstName());
        JdbcTemplate jdbcTemplateTwoDst = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateDstTwoName());
        String srcCmd = job.getSrcCommd();
        String dstCmd = job.getDstCommd();
        String dstTwoCmd = job.getDstTwoCommd();

        List<Map<String, Object>> srcData = jdbcTemplateSrc.queryForList(srcCmd);
        if (srcData.size() > 0) {
            List<Object[]> listDstTwo = new ArrayList<Object[]>();
            for (Map<String, Object> mp : srcData) {
                listDstTwo.add(mp.values().toArray());
            }
            int[] ans = jdbcTemplateTwoDst.batchUpdate(dstTwoCmd, listDstTwo);
        }

        List<Map<String, Object>> dstData = jdbcTemplateDst.queryForList(dstCmd);
        if (dstData.size() > 0) {
            List<Object[]> listDstTwo = new ArrayList<Object[]>();
            for (Map<String, Object> mp : srcData) {
                listDstTwo.add(mp.values().toArray());
            }
            int[] ans = jdbcTemplateTwoDst.batchUpdate(dstTwoCmd, listDstTwo);
        }
    }

    //6：A操作-B查询-C操作
    private void transDataFromDstToDstTwoBatch(Job job) {
        JdbcTemplate jdbcTemplateSrc = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateSrcName());
        JdbcTemplate jdbcTemplateDst = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateDstName());
        JdbcTemplate jdbcTemplateTwoDst = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateDstTwoName());
        String srcCmd = job.getSrcCommd();
        String dstCmd = job.getDstCommd();
        String dstTwoCmd = job.getDstTwoCommd();

        jdbcTemplateSrc.update(srcCmd);

        List<Map<String, Object>> dstData = jdbcTemplateDst.queryForList(dstCmd);
        if (dstData.size() > 0) {
            List<Object[]> listDstTwo = new ArrayList<Object[]>();
            for (Map<String, Object> mp : dstData) {
                listDstTwo.add(mp.values().toArray());
            }
            int[] ans = jdbcTemplateTwoDst.batchUpdate(dstTwoCmd, listDstTwo);
        }
    }

    //5: A查询-B操作-C操作
    private void transDataFromSrcToDstTwoBatch(Job job) {
        JdbcTemplate jdbcTemplateSrc = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateSrcName());
        JdbcTemplate jdbcTemplateDst = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateDstName());
        JdbcTemplate jdbcTemplateTwoDst = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateDstTwoName());
        String srcCmd = job.getSrcCommd();
        String dstCmd = job.getDstCommd();
        String dstTwoCmd = job.getDstTwoCommd();

        List<Map<String, Object>> srcData = jdbcTemplateSrc.queryForList(srcCmd);
        if (srcData.size() > 0) {
            List<Object[]> listDst = new ArrayList<Object[]>();
            for (Map<String, Object> mp : srcData) {
                listDst.add(mp.values().toArray());
            }
            int[] ans = jdbcTemplateDst.batchUpdate(dstCmd, listDst);
            int[] ansTwo = jdbcTemplateTwoDst.batchUpdate(dstTwoCmd, listDst);
        }
    }

    //4: A查询-B查询-C操作
    private void transDataFromSrcToDstBatchTwo(Job job) {
        JdbcTemplate jdbcTemplateSrc = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateSrcName());
        JdbcTemplate jdbcTemplateDst = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateDstName());
        JdbcTemplate jdbcTemplateTwoDst = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateDstTwoName());
        String srcCmd = job.getSrcCommd();
        String dstCmd = job.getDstCommd();
        String dstTwoCmd = job.getDstTwoCommd();

        List<Map<String, Object>> srcData = jdbcTemplateSrc.queryForList(srcCmd);
        if (srcData.size() > 0) {
            for (Map<String, Object> mp : srcData) {
                List<Map<String, Object>> dstData = jdbcTemplateDst.queryForList(dstCmd, mp.values().toArray());
                if (dstData.size() > 0) {
                    List<Object[]> listDst = new ArrayList<Object[]>();
                    for (Map<String, Object> dstmp : dstData) {
                        listDst.add(dstmp.values().toArray());
                    }
                    int[] ansTwo = jdbcTemplateTwoDst.batchUpdate(dstTwoCmd, listDst);
                }
            }
        }
    }

    //3：A查询-B操作
    private void transDataFromSrcToDstBatch(Job job) {
        JdbcTemplate jdbcTemplateSrc = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateSrcName());
        JdbcTemplate jdbcTemplateDst = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateDstName());
        String srcCmd = job.getSrcCommd();
        String dstCmd = job.getDstCommd();
        List<Map<String, Object>> srcData = jdbcTemplateSrc.queryForList(srcCmd);
        if (srcData.size() > 0) {
            List<Object[]> listDst = new ArrayList<Object[]>();
            for (Map<String, Object> mp : srcData) {
                listDst.add(mp.values().toArray());
            }
            int[] ans = jdbcTemplateDst.batchUpdate(dstCmd, listDst);
        }
    }

    //2：A/B/C操作
    private void updateDstData(Job job) {
        if (!(job.getTemplateSrcName() == null || job.getTemplateSrcName().equals("")) & !(job.getSrcCommd() == null || job.getSrcCommd().equals(""))) {
            JdbcTemplate jdbcTemplateSrc = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateSrcName());
            String srcCmd = job.getSrcCommd();
            jdbcTemplateSrc.update(srcCmd);
        }

        if (!(job.getTemplateDstName() == null || job.getTemplateDstName().equals("")) & !(job.getDstCommd() == null || job.getDstCommd().equals(""))) {
            JdbcTemplate jdbcTemplateDst = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateDstName());
            String dstCmd = job.getDstCommd();
            jdbcTemplateDst.update(dstCmd);
        }

        if (!(job.getTemplateDstTwoName() == null || job.getTemplateDstTwoName().equals("")) & !(job.getDstTwoCommd() == null || job.getDstTwoCommd().equals(""))) {
            JdbcTemplate jdbcTemplateTwoDst = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateDstTwoName());
            String dstTwoCmd = job.getDstTwoCommd();
            jdbcTemplateTwoDst.update(dstTwoCmd);
        }
    }

    //1：A查询-B操作 (普通)
    private void transDataFromSrcToDst(Job job) {
        JdbcTemplate jdbcTemplateSrc = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateSrcName());
        JdbcTemplate jdbcTemplateDst = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateDstName());
        String srcCmd = job.getSrcCommd();
        String dstCmd = job.getDstCommd();
        List<Map<String, Object>> srcData = jdbcTemplateSrc.queryForList(srcCmd);
        if (srcData.size() > 0) {
            for (Map<String, Object> mp : srcData) {
                jdbcTemplateDst.update(dstCmd, mp.values().toArray());
            }
        }
    }
}
